package com.daxia.ogmall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.ogmall.dto.JsonResultDTO;
import com.daxia.ogmall.dto.PayDTO;
import com.daxia.ogmall.service.PayService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/pay", produces="text/html;charset=UTF-8")
public class AdminPayController extends BaseController {

	@Autowired
	private PayService payService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			PayDTO dto = payService.load(id);
			map.put("n", dto);
		}
		return "admin/pay/pay_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(PayDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			payService.create(dto);
		} else {
			payService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		payService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	
	public String list(PayDTO dto, Map<String, Object> map, Page page) {
		List<PayDTO> dtos = payService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/pay/pay_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(PayDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<PayDTO> dtos = payService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (PayDTO pay : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", pay.getId());
            jsonObject.put("orderNo", pay.getOrderNo());
            jsonObject.put("payType", pay.getPayType());
            jsonObject.put("status", pay.getStatus());
            jsonObject.put("amount", pay.getAmount());
            jsonObject.put("bizType", pay.getBizType());
            jsonObject.put("paymentNo", pay.getPaymentNo());
	        jsonObject.put("user.nickName", pay.getUser().getNickName());    	
            jsonObject.put("isSuccess", pay.getIsSuccess());
            jsonObject.put("responseStatus", pay.getResponseStatus());
			jsonObject.put("responseTime", formatDate(pay.getResponseTime(), "yyyy-MM-dd HH:mm:ss"));	            	
            jsonObject.put("responseMsg", pay.getResponseMsg());
			jsonObject.put("createTime", formatDate(pay.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));	            	
	        jsonObject.put("order.orderNo", pay.getOrder().getOrderNo());    	
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
}
